package com.qx.ddx.activity;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.View;

import com.qx.ddx.DDXApplication;
import com.qx.ddx.R;
import com.qx.ddx.activity.adapter.FragmentAdapter;
import com.qx.ddx.activity.fragment.AboutFragment;
import com.qx.ddx.activity.fragment.CacheFragment;
import com.qx.ddx.activity.fragment.ClearFragment;
import com.qx.ddx.activity.fragment.FeedbackFragment;
import com.qx.ddx.activity.fragment.LanguageFragment;
import com.qx.ddx.activity.fragment.SetInfoFragment;
import com.qx.ddx.activity.fragment.SetRestaurantFragment;
import com.qx.ddx.activity.fragment.ThemeFragment;
import com.qx.ddx.activity.fragment.UpdateFragment;
import com.qx.ddx.util.SpInfo;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/4/4.
 */
public class SetActivity extends BaseFragmentActivity implements View.OnClickListener {
    private FragmentAdapter fragAdapter;
    private View setting_btn, theme_btn, info_btn, clear_btn, language_btn, software_btn, update_btn, cache_btn, feedback_btn;
    public View view;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_set);
        DDXApplication.getInstance().addActivity(this);
        view = findViewById(R.id.set_background);
        SharedPreferences sp = getSharedPreferences(SpInfo.Setting, Activity.MODE_PRIVATE);
        view.setBackgroundResource(sp.getInt(SpInfo.ThemeBg, R.color.set_background));
        List<Fragment> fragments = new ArrayList<Fragment>();
        fragments.add(new SetRestaurantFragment());
        fragments.add(new ThemeFragment());
        fragments.add(new SetInfoFragment());
        fragments.add(new ClearFragment());
        fragments.add(new LanguageFragment());
        fragments.add(new AboutFragment());
        fragments.add(new UpdateFragment());
        fragments.add(new CacheFragment());
        fragments.add(new FeedbackFragment());
        fragAdapter = new FragmentAdapter(this.getSupportFragmentManager(), R.id.set_content, fragments);
        fragAdapter.setFragment(mSelectedIndex);
        View return_btn = findViewById(R.id.set_return_btn);
        return_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SetActivity.this.finish();
            }
        });
        setting_btn = findViewById(R.id.set_setting);
        setting_btn.setOnClickListener(this);
        theme_btn = findViewById(R.id.set_theme);
        theme_btn.setOnClickListener(this);
        info_btn = findViewById(R.id.set_info);
        info_btn.setOnClickListener(this);
        clear_btn = findViewById(R.id.set_clear);
        clear_btn.setOnClickListener(this);
        language_btn = findViewById(R.id.set_language);
        language_btn.setOnClickListener(this);
        software_btn = findViewById(R.id.set_software);
        software_btn.setOnClickListener(this);
        update_btn = findViewById(R.id.set_update);
        update_btn.setOnClickListener(this);
        cache_btn = findViewById(R.id.set_cache);
        cache_btn.setOnClickListener(this);
        feedback_btn = findViewById(R.id.set_feedback);
        feedback_btn.setOnClickListener(this);
    }

    private int mSelectedIndex = 0;

    @Override
    public void onClick(View v) {
        switch (mSelectedIndex) {
            case 0:
                setNormalBackground(setting_btn);
                break;
            case 1:
                setNormalBackground(theme_btn);
                break;
            case 2:
                setNormalBackground(info_btn);
                break;
            case 3:
                setNormalBackground(clear_btn);
                break;
            case 4:
                setNormalBackground(language_btn);
                break;
            case 5:
                setNormalBackground(software_btn);
                break;
            case 6:
                setNormalBackground(update_btn);
                break;
            case 7:
                setNormalBackground(cache_btn);
                break;
            case 8:
                setNormalBackground(feedback_btn);
                break;

        }
        switch (v.getId()) {
            case R.id.set_setting:
                mSelectedIndex = 0;
                fragAdapter.setFragment(mSelectedIndex);
                break;
            case R.id.set_theme:
                mSelectedIndex = 1;
                fragAdapter.setFragment(mSelectedIndex);
                break;
            case R.id.set_info:
                mSelectedIndex = 2;
                fragAdapter.setFragment(mSelectedIndex);
                break;
            case R.id.set_clear:
                mSelectedIndex = 3;
                fragAdapter.setFragment(mSelectedIndex);
                break;
            case R.id.set_language:
                mSelectedIndex = 4;
                fragAdapter.setFragment(mSelectedIndex);
                break;
            case R.id.set_software:
                mSelectedIndex = 5;
                fragAdapter.setFragment(mSelectedIndex);
                break;
            case R.id.set_update:
                mSelectedIndex = 6;
                fragAdapter.setFragment(mSelectedIndex);
                break;
            case R.id.set_cache:
                mSelectedIndex = 7;
                fragAdapter.setFragment(mSelectedIndex);
                break;
            case R.id.set_feedback:
                mSelectedIndex = 8;
                fragAdapter.setFragment(mSelectedIndex);
                break;
        }
        setPressedBackground(v);
    }

    private void setPressedBackground(View v) {
        v.setBackgroundColor(SetActivity.this.getResources().getColor(R.color.set_left_list_item_pressed_background));
    }

    private void setNormalBackground(View v) {
        v.setBackground(SetActivity.this.getResources().getDrawable(R.drawable.set_list_item_normal));
    }
}
